package flc.ast.adapter;

import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.stark.mindmap.lib.R;
import com.stark.mindmap.lib.core.adapter.DrawInfo;
import com.stark.mindmap.lib.core.adapter.TreeViewAdapter;
import com.stark.mindmap.lib.core.adapter.TreeViewHolder;
import com.stark.mindmap.lib.core.line.BaseLine;
import com.stark.mindmap.lib.core.listener.TreeViewItemClick;
import com.stark.mindmap.lib.core.listener.TreeViewItemLongClick;
import com.stark.mindmap.lib.core.model.DefNodeData;
import com.stark.mindmap.lib.core.model.NodeModel;
import com.stark.mindmap.lib.core.model.TreeModel;

/**
 * Author：daizhongbin on 2023/8/14
 * Email：1027154338@qq.com
 * Desc:
 */
public class MyTreeViewAdapter extends TreeViewAdapter<DefNodeData> {

    private TreeViewItemClick<DefNodeData> itemClickListener;
    private TreeViewItemLongClick<DefNodeData> itemLongClickListener;
    private NodeModel<DefNodeData> curSelNodeModel;

    public void setItemClickListener(TreeViewItemClick<DefNodeData> itemClickListener) {
        this.itemClickListener = itemClickListener;
    }

    public void setItemLongClickListener(TreeViewItemLongClick<DefNodeData> itemLongClickListener) {
        this.itemLongClickListener = itemLongClickListener;
    }

    public NodeModel<DefNodeData> getCurSelNodeModel() {
        return curSelNodeModel;
    }

    public void setCurSelNodeModel(NodeModel<DefNodeData> curSelNodeModel) {
        this.curSelNodeModel = curSelNodeModel;
    }

    /**
     * 判断当前选中的节点是否为根节点
     *
     * @return
     */
    public boolean isCurSelNodeModelRoot() {
        return curSelNodeModel == getTreeModel().getRootNode();
    }

    @Override
    public TreeViewHolder<DefNodeData> onCreateViewHolder(@NonNull ViewGroup viewGroup, NodeModel<DefNodeData> model) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_my_tree_view, viewGroup, false);
        TreeViewHolder<DefNodeData> holder = new TreeViewHolder<>(view, model);
        return holder;
    }

    @Override
    public void setTreeModel(TreeModel<DefNodeData> treeModel) {
        if (treeModel != null) {
            curSelNodeModel = treeModel.getRootNode();
        }
        super.setTreeModel(treeModel);
    }

    @Override
    public void onBindViewHolder(@NonNull TreeViewHolder<DefNodeData> holder) {
        View itemView = holder.getView();
        DefNodeData nodeData = holder.getNode().getValue();
        TextView tvContent = itemView.findViewById(R.id.tvContent);
        tvContent.setText(nodeData.text);
        tvContent.setTextSize(nodeData.textSize);
        tvContent.setTextColor(nodeData.textColor);
        if (holder.getNode() == curSelNodeModel) {
            tvContent.setBackgroundColor(Color.parseColor("#162057"));
        } else {
            tvContent.setBackgroundColor(Color.parseColor("#769CFB"));
        }
        itemView.setOnClickListener(v -> {
            if (itemClickListener != null) {
                itemClickListener.onItemClick(v, holder.getNode());
            }
            curSelNodeModel = holder.getNode();
            notifyDataSetChange();
        });
        itemView.setOnLongClickListener(v -> {
            if (itemLongClickListener != null) {
                itemLongClickListener.onLongClick(v, holder.getNode());
            }
            curSelNodeModel = holder.getNode();
            notifyDataSetChange();
            return true;
        });
    }

    @Override
    public BaseLine onDrawLine(DrawInfo drawInfo) {
        return null;
    }

}
